﻿using System;
using System.Data;
using System.Xml;

namespace TextEditor
{
    

    public class A_SaveDBFile : A_Action
    {
        public OnSaveHandler OnSave = null;

        public override string ActionName()
        {
            return "savedbfile";
        }

        public override bool Execute()
        {
            XmlDocument myDoc = new XmlDocument();
            myDoc.PreserveWhitespace = true;
            base.myOwnerDocument.Info.SaveMode = 0;
            base.myOwnerDocument.ToXMLDocument(myDoc);
            ET_Document myRecord = new ET_Document();
            myRecord.SetKeyWord(base.myOwnerDocument.Info.ID);
            myRecord.KeyPreFix = ZYKBBuffer.Instance.KeyPreFix;
            if (myRecord.isKeyEnable() && base.myOwnerDocument.DataSource.DBConn.TestRecordExist(myRecord))
            {
                myRecord.DataState = DataRowState.Modified;
            }
            else
            {
                myRecord.DataState = DataRowState.Added;
                myRecord.ObjectID = null;
            }
            myRecord.ObjectName = base.myOwnerDocument.Info.Title;
            myRecord.ObjectData = myDoc.DocumentElement.OuterXml;
            if (base.myOwnerDocument.DataSource.DBConn.UpdateRecordAuto(myRecord))
            {
                base.myOwnerDocument.Info.ID = myRecord.ObjectID.ToString();
                base.myOwnerDocument.Modified = false;
                if (this.OnSave != null)
                {
                    this.OnSave();
                }
                return true;
            }
            return false;
        }
    }
}

